﻿/* $RCSFile: MenuHelper.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/Helpers/MenuHelper.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:28  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

using AddonNice.Diagnostics;
using AddonNice.Configuration;
using AddonNice.Core;
using AddonNice.Settings;

using Trace2    =   System.Diagnostics.Trace;

namespace AddonNice.Helpers
{
    public class MenuHelper
    {
		
        public static string GiveMeUrl(string tab, int id,ADNPageFlags Flags,bool UseTabNameInUrl) 
		{
            if ( NavTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]MenuHelper GiveMeUrl tab: {0}, id: {1}, UseTabNameInUrl: {2}, Flags: {3}.",
                    new object[]{tab,id,UseTabNameInUrl,Flags}),NavTraceSwitch.Sw.Info);
			DynPageLoc loc      =   DynPageLoc.Normal;
            bool keepTargetPage =   false;
            if ( (Flags & ADNPageFlags.LoginLoc) == ADNPageFlags.LoginLoc )
                loc             =    DynPageLoc.Login;
            if ( (Flags & ADNPageFlags.SecureLoc) == ADNPageFlags.SecureLoc )
                loc             =    DynPageLoc.Secured;
            if ( (Flags & ADNPageFlags.KeepTargetPage) == ADNPageFlags.KeepTargetPage )
                keepTargetPage  =    true;
            if (!UseTabNameInUrl) 
                return HttpUrlBuilder.BuildUrl(loc,id);
            tab                 =   Utils.RemoveDiacritics(tab);
			string auxtab       =   string.Empty;
			foreach (char c in tab)
				if (char.IsLetterOrDigit(c)) 
                    auxtab      +=  c; 
                else auxtab     +=  "_";
            if ( NavTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]MenuHelper giveMeUrl tab: {0}, id: {1}, auxtab: {2}, loc: {3}",
                    new object[]{tab, id, auxtab,loc}),NavTraceSwitch.Sw.Info);
            // CS 13/2/7 to avoid links to _.aspx
            if (auxtab == "_")
                return string.Empty;
            return HttpUrlBuilder.BuildUrl(string.Format("~/{0}.aspx",auxtab),keepTargetPage, loc,id);
		}

    }
}
